package cn.cxj.pet.controller;

import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.pet.domain.PetType;
import cn.cxj.pet.query.PetTypeQuery;
import cn.cxj.pet.service.IPetTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宠物类型接口
 * @author 陈鑫杰
 */
@RestController
@RequestMapping("/petType")
@Api(tags = "宠物类型接口",description = "宠物类型接口详细描述")
public class PetTypeController {

    @Autowired
    private IPetTypeService petTypeService;

    /**
     * 新增或者修改 POST
     * @param petType
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "宠物类型添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "新增或者修改方法")
    public AjaxResult addOrUpdate(@RequestBody PetType petType){
        try {
            if (petType.getId() == null) {
                petTypeService.add(petType);
            } else {
                petTypeService.update(petType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id删除 DELETE
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(operateType = "根据id删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            petTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @LogAnnotation(operateType = "基于id查询")
    public PetType findById(@PathVariable("id") Long id){
        return petTypeService.queryById(id);
    }

    /**
     * 查询全部 GET
     * @return
     */
    @GetMapping("/list")
    @LogAnnotation(operateType = "查询全部")
    public List<PetType> findAll(){
        return petTypeService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query
     * @return
     */
    @PostMapping("/page")
    @LogAnnotation(operateType = "分页和高级查询")
    public PageList<PetType> pageQuery(@RequestBody PetTypeQuery query){
        return petTypeService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            petTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }
}
